package org.mysfit.ctp4j.core.service.impl;

import java.math.BigDecimal;

import org.mysfit.ctp4j.core.MyCThostFtdcMdSpi;
import org.mysfit.ctp4j.core.service.CommonService;
import org.mysfit.ctp4j.core.service.FkDataService;
import org.mysfit.ctp4j.core.service.MarketService;
import org.mysfit.ctp4j.core.service.SkDataService;
import org.mysfit.ctp4j.infra.entity.SkData;
import org.mysfit.ctp4j.infra.mapper.SkDataMapper;
import org.mysfit.ctp4j.infra.pojo.CtpConfig;
import org.mysfit.ctp4j.infra.pojo.CtpUser;
import org.mysfit.ctp4j.infra.utils.TradeDataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ctp.thosttraderapi.CThostFtdcDepthMarketDataField;
import ctp.thosttraderapi.CThostFtdcMdApi;

/**
 * <p>
 * sk 服务实现类
 * </p>
 *
 * @author long.gf
 * @since 2022-12-02
 */
@Service
public class MarketServiceImpl implements MarketService {
	
	@Autowired
	private SkDataMapper skMapper;

	@Autowired
	private FkDataService fkService;
	
	@Autowired
	private SkDataService skService;
	
	@Autowired
	private CommonService commonService;
	
	@Override
	public void subscribe(String investorId) {
		CtpConfig ctpConfig = commonService.getConfig();
		CtpUser currCtpUser = commonService.getUser(investorId);
		CThostFtdcMdApi mdApi = CThostFtdcMdApi.CreateFtdcMdApi();
		MyCThostFtdcMdSpi mdSpi = new MyCThostFtdcMdSpi(currCtpUser, mdApi, this);
		mdApi.RegisterSpi(mdSpi);
		mdApi.RegisterFront(ctpConfig.getMarketFront());
		mdApi.Init();
		mdApi.Join();
	}
	
	
	@Override
	public void addData(CThostFtdcDepthMarketDataField data)  {
		if (data != null) {
			String fid = data.getInstrumentID();
			String day = data.getTradingDay().substring(0,4)+"-"+data.getTradingDay().substring(4,6)+"-"+data.getTradingDay().substring(6);
			String time = data.getUpdateTime();
			int millisec = data.getUpdateMillisec();
			if(millisec == 500) {
				if(!TradeDataUtils.getInstance(fkService, skService).checkUk(fid, day, time)) {
					return ;
				}
				String volKey = "vol" + "_"+fid ;
				String amtKey = "amt" + "_"+fid ;
				BigDecimal lastVol = null == TradeDataUtils.LAST_MK_MAP.get(volKey) ? BigDecimal.ZERO : TradeDataUtils.LAST_MK_MAP.get(volKey);
				BigDecimal lastAmount = null == TradeDataUtils.LAST_MK_MAP.get(amtKey) ? BigDecimal.ZERO : TradeDataUtils.LAST_MK_MAP.get(amtKey);
				SkData sk = new SkData();
				sk.setFid(fid);
				sk.setFp(new BigDecimal(data.getLastPrice()));
				sk.setFv(new BigDecimal(data.getVolume()).subtract(lastVol));
				sk.setFa(new BigDecimal(data.getTurnover()).subtract(lastAmount));
				sk.setTotalFv(new BigDecimal(data.getVolume()));
				sk.setTotalFa(new BigDecimal(data.getTurnover()));
				sk.setFtime(day + " " + time);
				skMapper.insert(sk);
				TradeDataUtils.getInstance(fkService, skService).insertFk(sk);
				TradeDataUtils.LAST_MK_MAP.put(volKey, new BigDecimal(data.getVolume()));
				TradeDataUtils.LAST_MK_MAP.put(amtKey, new BigDecimal(data.getTurnover()));
				TradeDataUtils.getInstance(fkService, skService).refershSk(sk);
				TradeDataUtils.getInstance(fkService, skService).refershFk(sk);
			}
         } 
	}
	
}
